package com.yx.enterresourceplanning.controller;

import com.yx.enterresourceplanning.entity.User;
import com.yx.enterresourceplanning.service.TokenService;
import com.yx.enterresourceplanning.service.UserService;
import com.yx.enterresourceplanning.service.serviceImpl.TokenServiceImpl;
import com.yx.enterresourceplanning.utils.ResponseCode;
import com.yx.enterresourceplanning.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    private final TokenServiceImpl tokenServiceImpl;

    public UserController(TokenServiceImpl tokenServiceImpl) {
        this.tokenServiceImpl = tokenServiceImpl;
    }
    @PostMapping("/login")
    public Result<?> login(@RequestParam(name="username") String username, @RequestParam(name="password") String password) {
        String token = UUID.randomUUID().toString();
        User user = userService.login(username, password);
        if (user == null) {
            return Result.fail(ResponseCode.USER_NOT_FOUND);
        }
        
        // 登录成功后保存token，有效期30秒
        tokenServiceImpl.saveToken("token:"+username, token, 15, TimeUnit.MINUTES);
        System.out.println("Login successful, token: " + token);
        
        // 创建包含用户信息和token的响应
        class LoginResponse {
            private User user;
            private String token;
            
            public LoginResponse(User user, String token) {
                this.user = user;
                this.token = token;
            }
            
            public User getUser() {
                return user;
            }
            
            public String getToken() {
                return token;
            }
        }
        
        return Result.success(ResponseCode.SUCCESS, new LoginResponse(user, token));
    }
    @PostMapping("/add")
    public Result<Integer> addUser(@RequestParam(name = "user_id") String user_id, @RequestParam(name = "name")
                                String name, @RequestParam(name = "username") String username, @RequestParam(name = "password")
                                String  password, @RequestParam(name = "dep_id")String dep_id, @RequestParam(name = "role")
                                String  role) {
        return Result.success(ResponseCode.SUCCESS, userService.addUser(user_id, name, username, password, dep_id, role));
    }
    @GetMapping("/profile")
    public Result<User> getUserProfile(@RequestParam(name = "username") String username, @RequestParam(name = "token") String token) {
        boolean isValid = tokenServiceImpl.validateToken("token:" + username, token);
        if (!isValid) {
            return Result.fail(ResponseCode.TOKEN_EXPIRED, "请重新登录");
        }
        User user = userService.findByUsername(username);
        if (user == null) {
            return Result.fail(ResponseCode.USER_NOT_FOUND);
        }
        return Result.success(ResponseCode.SUCCESS, user);
    }


}